<template>
	<div class="login-from">
		<header>
			<h1 style="text-align: center;">中介管理系统</h1>
			<p>Intermediary's administration system</p>
		</header>
		<el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="60px" class="demo-ruleForm">
			<el-form-item label="账号:" prop="u_account_number">
				<el-input v-model="ruleForm.u_account_number" @keyup.enter.native="submitForm('ruleForm')"></el-input>
			</el-form-item>
			<el-form-item label="密码:" prop="u_password">
				<el-input type="password" v-model="ruleForm.u_password" @keyup.enter.native="submitForm('ruleForm')"></el-input>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" @click="submitForm('ruleForm')">确定</el-button>
			</el-form-item>
		</el-form>
	</div>
</template>

<script>
	import "../style/login.css" 
	import config from '../config/userconf.js' 
  export default {
    data() {
      return {
        ruleForm: {
          u_account_number: '',
          u_password: ''
        },
        rules: {
          u_account_number: [
                { required: true, message: '请输入用户账号', trigger: 'blur' }
          ],
		      u_password: [
            { required: true, message: '请输入密码', trigger: 'blur' },
            { min: 6, message: '密码不少于6位', trigger: 'blur' }
          ],
        }
      };
    },
    methods: {
      submitForm(formName,data=Object()) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            config.login(this.ruleForm).then((res)=>{
              if (res.data.code ==0) {
                console.log(res.data.data);
                //存储登陆信息
                window.localStorage.usernames = this.ruleForm.u_account_number;

                for(var k in res.data.data){
                  window.localStorage.setItem(k, res.data.data[k]);
                }
                
                window.localStorage.setItem('user_down', JSON.stringify(res.data.data.user_down) );

                //上班打卡
                config.signin.begin(data,(state,res)=>{
                  if (state == 'success'&&res.data.code == 0){  //打卡成功!
                      let userinfo = res.data.data.userinfo;
                      let worktime = res.data.data.worktime;
                      let info = res.data.data.info;
                      //查看是否有房客源提醒
                      config.signin.remind(data,(state,res)=>{
                          if (state == 'success'&&res.data.code == 0){
                              // console.log(res);
                              // console.log(userinfo,worktime,info);
                              this.$router.push({
                                path:'/room',
                                query:{
                                  userinfo: userinfo,
                                  worktime: worktime,
                                  info: info,
                                  remindstatus: 1,
                                }
                              });   //跳转至房源列表页    并携带打卡成功数据+房客源提醒数据
                              
                          }else if (state == 'success'&&res.data.code != 0){
                              if(res.data.code == -413){  //暂时没有业务提醒
                                console.log(userinfo,worktime,info);
                                  this.$router.push({
                                    path:'/room',
                                    query:{
                                      userinfo: userinfo,
                                      worktime: worktime,
                                      info: info,
                                    }
                                  });   //跳转至房源列表页    并携带打卡成功数据
                              }else{
                                  this.$message({
                                      type: 'info',
                                      message: res.data.msg
                                  });
                              }
                              
                          }
                      })
                  }else if(state == 'success'&&res.data.code != 0){
                    if(res.data.code == -409){  //已打卡
                      //查看是否有房客源提醒
                      config.signin.remind(data,(state,res)=>{
                          if (state == 'success'&&res.data.code == 0){
                              // console.log(res);
                              this.$router.push({
                                path:'/room',
                                query:{
                                  remindstatus: 1,
                                }
                              });   //跳转至房源列表页    并携带打卡成功数据+房客源提醒数据
                              
                          }else if (state == 'success'&&res.data.code != 0){
                              if(res.data.code == -413){  //暂时没有业务提醒
                                  this.$router.push({name:'room'});   //跳转至房源列表页
                              }else{
                                  this.$message({
                                      type: 'info',
                                      message: res.data.msg
                                  });
                              }
                              
                          }
                      })
                    }else{
                      this.$message({
                          type: 'info',
                          message: res.data.msg
                      });
                    }
                      
                  }
                })
                
              }else{
                this.$message({
                    type: 'info',
                    message: res.data.msg
                });
              }
            }).catch((e)=>{
              console.log(e)
            })
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    },
	  mounted (){
        this.$nextTick(function(){
           this.ruleForm.u_account_number =  window.localStorage.usernames?window.localStorage.usernames:''
        })
    },
  }
</script>
